home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr46 / multi_c1.zip / DEMO.CPP < prev    next >
C/C++ Source or Header  |  1992-11-08  |  12KB  |  610 lines

  1. /**********************************************************************
  2.  
  3.    = = = A General Demo of the MultiApp Core V1.0 = = =
  4.  
  5.  
  6.    Copyright 1992 by Jeff Heaton, MicroGenesis Software
  7.  
  8. Compilers Tested On:
  9.  
  10.  Borland C++ V3.0     - IBM DOS/WINDOWS
  11.  Turbo C++ V2.0      - IBM DOS/WINDOWS
  12.  Symantec Think C 5.0(with objects) - MAC System 6&7
  13.  
  14.  This demo shows some of the major features of the MultiApp core.
  15. The four demos are: FILEIO, Atomic Structures, Communications and timing.
  16. Because MultiApp core has ABSOLUTELY NO user interface one had to be provided
  17. in this demo.  Unfortunately ANSI C/C++ does not have an advanced enough
  18. user interface for this demo, so some compiler specific commands had to be
  19. used.  These are masked with #ifdef's using the keywords of IBM and MAC.
  20. The keyword of msWINDOWS may also be used with the windows version of
  21. MULTI-APP core.
  22.  
  23. MicroGenesis Software
  24. P.O. Box 25534
  25. St. Louis, MO 63125
  26.  
  27. CIS: 76476,1701
  28.  
  29. VOICE: (314) 638-2506
  30. BBS  : (314) 638-1731
  31.  
  32.  
  33.  MUST COMPILE UNDER LARGE MEMORY MODEL IF ON DOS
  34.  
  35.  **********************************************************************/
  36.  
  37. // ANSI-C comapt includes
  38. #include <stdio.h>
  39. #include <ctype.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42.  
  43. #include "multi_c.h"
  44.  
  45. // Compiler specific includes
  46. #ifdef MAC
  47. #include <console.h>
  48. #endif
  49.  
  50. #ifdef IBM
  51. #include <conio.h>
  52. #include <alloc.h>
  53. #endif
  54.  
  55.  
  56. // Even the ANSI-C interface cannot provide a system-independant enough
  57. // interface for this demo.
  58. int waiting=-1;// Character that is waiting to be read
  59.  
  60.  
  61. int key_get(void)// Get a SINGLE key stroke
  62. {
  63. #ifdef IBM
  64.  return getch();
  65. #endif
  66. #ifdef MAC
  67.  
  68.  if(waiting>0)
  69.   {
  70.   char c=waiting;
  71.   waiting=-1;
  72.   return c;
  73.   }
  74.  return getchar();
  75. #endif
  76. }
  77.  
  78. int key_hit(void)// Has the user hit a key?
  79. {
  80. #ifdef IBM
  81.  return kbhit();
  82. #endif
  83. #ifdef MAC
  84.  if(waiting>0)
  85.   return 1;
  86.  
  87.  waiting=getchar();
  88.  if(waiting>0)
  89.   return 1;
  90.  else return 0;
  91. #endif
  92. }
  93.  
  94.  
  95. // This is called internally by MultiApp when ALP times out.
  96. void alp_failed(void)
  97. {
  98.  printf("\n\nLine noise too bad, ALP connection lost.\n\n");
  99. }
  100.  
  101.  
  102.  
  103.  
  104. /************************************************************************
  105.  
  106. Demo for file sharing
  107.  
  108.  
  109.  ************************************************************************/
  110. void file_demo()
  111. {
  112. FILEIO *fp;
  113.  
  114.  printf("\n\nFILEIO/File Sharing Demo\n\n");
  115.  fp=new FILEIO;
  116.  
  117.  // Open file for read/write, which allows no shared access
  118.  // If we cannot gain that access now, then open_file waits for such
  119.  // access to become available.
  120.  fp->open_file("","DEMO.TXT",'+',"TEXT","DEMO");
  121.  
  122.  // Dump the file to stdout
  123.  while( !fp->end() )
  124.   putchar(fp->get(NULL));
  125.  
  126.  printf("\n\nPress any key so that other tasks may access this file.");
  127.  key_get();
  128.  
  129.  // Release file
  130.  fp->close_file();
  131.  delete fp;
  132. }
  133.  
  134.  
  135.  
  136. /************************************************************************
  137.  
  138. Demo For communications
  139.  
  140.  
  141.  ************************************************************************/
  142.  
  143. // Strip is a useful function that will remove path information from a
  144. // file name.  This works for both Macintosh and IBM File formats.
  145. // Example for IBM:
  146. // IN - C:\WINDOWS\SYSTEM\WIN.INI
  147. //  OUT- WIN.INI
  148. //
  149. // Example for Macintosh:
  150. //
  151. //  IN - MacintoshHD:System:system
  152. // OUT - system
  153. void strip(char *str)
  154. {
  155. char *ptr=str,*s1,*s2;
  156.  
  157.  s1=strchr(str,':');
  158.  s2=strchr(str,'\\');
  159.  
  160.  while( (s1!=NULL) || (s2!=NULL) )
  161.   {
  162.   if(s1>s2)
  163.    ptr=s1+1;
  164.   else
  165.    ptr=s2+1;
  166.  
  167.   s1=strchr(ptr,':');
  168.   s2=strchr(ptr,'\\');
  169.   }
  170.  strcpy(str,ptr);
  171. }
  172.  
  173.  
  174.  
  175. // rec_file - This function is called whenever a ctrl-A character is
  176. // received over the ALP link.  ctrl-A is simply the value I selected for this
  177. // demo to start a file transfer-- it is not a multi-app standard.  After
  178. // the ctrl-a the name and size(separated by carriage returns) should follow.
  179. //
  180. // This file transfer is just a straight binary dump.  There is no block size
  181. // implied at this level.  All error detection/compression is handled by
  182. // the ALP object(or any ALP-compatible object, which could use for example
  183. // netbios/appletalk rather than serial).
  184. void rec_file(ALP *com)
  185. {
  186. char name[80],size[80];
  187. FILEIO *fp;
  188. long todo;
  189. char str[80];
  190. int k=0;
  191. float f;
  192.  
  193.  com->getstr(name,79);// Needed values from remote
  194.  com->getstr(size,79);
  195.  
  196.  printf("\n\nReceiving: %s(%s)\n",name,size);
  197.  fp=new FILEIO;
  198.  fp->open_file("",name,'w',"TEXT","ttxt");// Save as teach text(if mac)
  199.  strip(name);
  200.  todo=atol(size);
  201.  
  202.  while(todo>0)
  203.   {
  204.   k=0;
  205.  
  206.   while(com->hit() && (k<512) )
  207.    {
  208.    fp->put(com->get(NULL));// This statement writes out every byte
  209.    todo--;
  210.    k++;
  211.  
  212.    if(key_hit())// Check for a local abort
  213.     {
  214.     if(key_get()==ESC)
  215.      {
  216.      com->putstr("~~~~");
  217.      break;
  218.      }
  219.     }
  220.  
  221.    }
  222.  
  223.   printf("%s: %ld         \r",name,todo);// Status line
  224.   com->update();
  225.   }
  226.  fp->close_file();
  227.  delete fp;
  228. }
  229.  
  230.  
  231. void send_file(ALP *com)
  232. {
  233. long l;
  234. char size[80];
  235. BYTE ch;
  236. FILEIO *fp;
  237. char name[80];
  238.  
  239.  if(com->no_alp)// This transfer wont work so well without ALP
  240.   {
  241.   printf("\n\nYou must establish an ALP link to transfer.\n\n");
  242.   return;
  243.   }
  244.  
  245.  printf("\nSend what file: ");// Input filename
  246.  gets(name);
  247.  
  248.  // Warn Macintosh users
  249. #ifdef MAC
  250.  printf("\n\nThis demo will ONLY transfer the data fork of a file!\n");
  251.  printf("Transparent resource fork transfer is planed for the next version.\n");
  252. #endif
  253.  
  254.  fp=new FILEIO;
  255.  fp->open_file("",name,'r',"","");
  256.  l=fp->file_length();
  257.  if(l<=0)
  258.   {
  259.   printf("\nFile Not Found\n");
  260.   delete fp;
  261.   return;
  262.   }
  263.  
  264.  strip(name);
  265.  com->printf("%c%s\r%ld\r",1,name,l);// Prompt other side to begin rec.
  266.  com->update();// Clear out the ALP buffer
  267.  while(com->intrans)
  268.   com->update();
  269.  do
  270.   {
  271.  
  272.   // This is how to read DIRECTLY into ALP's outbound buffer.
  273.   // WARNING: Make SURE nothing is in transit before you do this.
  274.   //      It is accomplished here in the loop while loop below.
  275.  
  276.   fp->fread(com->str,1024);
  277.  
  278.   com->p=l;// determine length(and short length on last "block")
  279.   if(l>1024)
  280.    com->p=1024;
  281.   l-=com->p;
  282.  
  283.   com->update();// Cause packet to be sent
  284.  
  285.   while(com->intrans)
  286.    com->update();// Loop until its done with that packet(see WARNING)
  287.  
  288.   printf("%s:%ld        \r",name,l);// Display status
  289.  
  290.   if(com->hit())// Check for remote abort
  291.    {
  292.    com->get(&ch);
  293.    if(ch=='~')
  294.     {
  295.     com->update();
  296.     break;
  297.     }
  298.    }
  299.  
  300.   if(key_hit())// Check for a local abort
  301.    {
  302.    if(key_get()==ESC)
  303.     {
  304.     com->putstr("~~~~");
  305.     break;
  306.     }
  307.    }
  308.  
  309.   } while( (l>0) && (!com->hit()) );
  310.  delete fp;
  311. }
  312.  
  313.  
  314.  
  315. void comm_demo()
  316. {
  317. ALP *com;
  318. char str[80];
  319. int done=0;
  320.  
  321.  com=new ALP;
  322.  com->direct=1;
  323.  
  324.  printf("\n\nTTY Terminal\n\n");
  325.  printf("Press ESC to quit, ~ to establish ALP, \\ to send a file.\n");
  326.  printf("Use comport(1-2)> ");
  327.  
  328.  gets(str);
  329.  if( com->init(atoi(str)) )
  330.     {
  331.     printf("\n\nThat comport does not exist\n\n");
  332.     return;
  333.     }
  334.  
  335.  
  336.  printf("Baud rate(300-38400)> ");
  337.  gets(str);
  338.  com->baud(atol(str));
  339.  
  340. // Turn mac console back to single character terminal
  341. #ifdef MAC
  342.  csetmode(C_RAW, stdin);
  343. #endif
  344.  
  345.  com->putstr("ATZ{");
  346.  
  347.  while(!done)
  348.   {
  349.   int ch;
  350.  
  351.   if(key_hit())
  352.    {
  353.    ch=key_get();
  354.  
  355.    if(ch==ESC)
  356.     done++;
  357.    else
  358.    if(ch=='~')
  359.     {
  360.     if(com->check())
  361.      printf("\n\nALP CONNECT\n");
  362.     }
  363.    else
  364.     if(ch=='\\')
  365.      send_file(com);
  366.    else
  367.     com->put(ch);
  368.    }
  369.  
  370.   while( com->hit() )
  371.    {
  372.    char ch=com->get(NULL);
  373.  
  374.    if(ch==1)
  375.     rec_file(com);
  376.    else putchar(ch);
  377.    }
  378.  
  379.   com->update();
  380.   }
  381.  delete com;
  382.  
  383. // Turn mac console back to normal
  384. #ifdef MAC
  385.  csetmode(C_ECHO, stdin);
  386. #endif
  387. }
  388.  
  389. /************************************************************************
  390.  
  391. Demo For Atomic Data Structures
  392.  
  393.  
  394.  ************************************************************************/
  395.  
  396. // Declare a simple atomic class to hold a string
  397. class DEMO_ITEM:public ATOM
  398. {
  399.  public:
  400.  DEMO_ITEM();
  401.  ~DEMO_ITEM();
  402.  
  403.  void set(char *str);
  404.  char *value(void);
  405.  
  406.  
  407.  private:
  408.   char *data;
  409. };
  410.  
  411. DEMO_ITEM::DEMO_ITEM()
  412. {
  413.  data=NULL;
  414. }
  415.  
  416. DEMO_ITEM::~DEMO_ITEM()
  417. {
  418.  if(data!=NULL)
  419.   free(data);
  420. }
  421.  
  422. void DEMO_ITEM::set(char *str)
  423. {
  424.  if(data!=NULL)
  425.   free(data);
  426.  
  427.  data=(char*)malloc( (sizeof(char)*strlen(str))+1 );
  428.  strcpy(data,str);
  429. }
  430.  
  431. char *DEMO_ITEM::value(void)
  432. {
  433.  return data;
  434. }
  435.  
  436.  
  437.  
  438. // Declare a simple atomic list to hold a list of strings
  439. class DEMO_LIST:public LIST
  440. {
  441.  public:
  442.  void insert(char *str);
  443.  void dump(void);
  444.  int comp(ATOM *a1,ATOM *a2);
  445.  
  446. };
  447.  
  448. void DEMO_LIST::insert(char *str)
  449. {
  450. DEMO_ITEM *item;
  451.  
  452.  item=new DEMO_ITEM;
  453.  item->set(str);
  454.  add((ATOM*)item);
  455. }
  456.  
  457. int DEMO_LIST::comp(ATOM *a1,ATOM *a2)
  458. {
  459. DEMO_ITEM *i1,*i2;
  460.  
  461.  i1=(DEMO_ITEM*)a1;
  462.  i2=(DEMO_ITEM*)a2;
  463.  
  464.  return(strcmp(i1->value(),i2->value())<0);
  465. }
  466.  
  467.  
  468. void DEMO_LIST::dump(void)
  469. {
  470.  printf("\nThe list contains:\n\n");
  471.  first_element();
  472.  while(current!=NULL)
  473.   {
  474.   DEMO_ITEM *itm=(DEMO_ITEM*)current;
  475.  
  476.   printf("%s\n",itm->value());
  477.   forward();
  478.   }
  479. }
  480.  
  481. void atomic_demo()
  482. {
  483. DEMO_LIST *list;
  484. char str[80];
  485.  
  486.  printf("\nPlease enter some words on the next few lines, <CR> on a blank\n");
  487.  printf("Line to sort list.\n");
  488.  
  489.  list=new DEMO_LIST;
  490.  
  491.  do {
  492.   gets(str);
  493.   if(*str)
  494.    list->insert(str);
  495.   } while(*str);
  496.  
  497.  list->dump();
  498.  delete list;
  499. }
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506. /************************************************************************
  507.  
  508. Demo platform-independant timers
  509.  
  510.  
  511.  ************************************************************************/
  512.  
  513.  
  514. void timers_demo()
  515. {
  516. long tm1,tm2,tm3;
  517. int time1,time2,time3;
  518. char str[80];
  519. int done=0;
  520.  
  521.  printf("\nPlease enter a time to wait in 1/10ths of a second>");
  522.  gets(str);
  523.  printf("\n\nPauseing for %s 1/10ths of a second..",str);
  524.  sleep(atoi(str));
  525.  
  526.  
  527.  printf("\n\nPlease enter a value for timer 1(10ths of a second)> ");
  528.  gets(str);
  529.  time1=atoi(str);
  530.  
  531.  printf("\n\nPlease enter a value for timer 2(10ths of a second)> ");
  532.  gets(str);
  533.  time2=atoi(str);
  534.  
  535.  printf("\n\nPlease enter a value for timer 3(10ths of a second)> ");
  536.  gets(str);
  537.  time3=atoi(str);
  538.  
  539.  tm1=timer_set(time1);// Set all 3 as close as possible to same time
  540.  tm2=timer_set(time2);
  541.  tm3=timer_set(time3);
  542.  
  543.  while(!done)
  544.   {
  545.   if(timer_check(tm1))
  546.    if(timer_check(tm2))
  547.     if(timer_check(tm3))
  548.      done=1;
  549.  
  550.   printf("Timer 1: %s  Timer 2: %s  Timer 3: %s\n",
  551.    timer_check(tm1)?"Stop":"Running",
  552.    timer_check(tm2)?"Stop":"Running",
  553.    timer_check(tm3)?"Stop":"Running");
  554.   }
  555. }
  556.  
  557.  
  558.  
  559. void main(void)
  560. {
  561.  printf("\n\n\nSorry about the cheap user interface, but this is a demo of\n");
  562.  printf("MultiApp core, and for simplicity the ANSI C++ interface has been used\n\n");
  563.  
  564.  for(;;)
  565.   {
  566.   char str[80];// To hold user's menu selection
  567.   char *ptr;// To strip off any leading spaces
  568.  
  569.   printf("\n\nMultiApp Core Demo V1.0 Copyright 1992 by Jeff Heaton, MicroGenesi\n");
  570.   printf("\nPlease select the demo you wish to see:\n");
  571.   printf("1> Files and file sharing\n");
  572.   printf("2> Communications\n");
  573.   printf("3> Atomic structures\n");
  574.   printf("4> Timers\n");
  575.   printf("Q> Quit demo\n");
  576.  
  577.   gets(str);
  578.   ptr=str;
  579.  
  580.   while(*ptr==' ')// Remove leading space
  581.    ptr++;
  582.  
  583.   switch( toupper(*ptr) )
  584.    {
  585.    case '1':
  586.     file_demo();
  587.     break;
  588.  
  589.    case '2':
  590.     comm_demo();
  591.     break;
  592.  
  593.    case '3':
  594.     atomic_demo();
  595.     break;
  596.  
  597.    case '4':
  598.     timers_demo();
  599.     break;
  600.  
  601.    case 'Q':
  602.     return;
  603.  
  604.    default:
  605.     printf("\a\n\nInvalid Entry\n\n");
  606.     break;
  607.    }
  608.   }
  609. }
  610.